<?php

class Uuch_ManageController extends Uuch_BaseController {

    private $cdn = 'http://cdn.uuch.com';

    const COOKIE_USERNAME = '__xxadnnx';
    const COOKIE_IP = '__xxadppx';
    const COOKIE_TIME = '__xxadttx';

    public function init() {
        $this->_helper->layout->setLayout('admin');
        $this->view->cnd_org = $this->cdn;
        $this->view->cdn = $this->cdn;
        $this->view->topNavigation = $this->getTopNavigation();
        $controllers = $this->getRequest()->getParam('controller');
        $actions = $this->view->mainnav2 = $this->_getParam('action');
        Application_Model_checkip::getbyip(0);
        if (!$this->isLogin()) {
            $this->_redirector = $this->_helper->getHelper('Redirector');
            $url = $this->curPageURL();
            $this->_redirector->gotoUrl('/admini/managelogin/index?returnUrl=' . urlencode($url));
        }
        $roleid = Uuch_Util::getRoleId();
        if($roleid==100 && $controllers!='agent'){
               $this->message("没有权限访问", "", "/admini/agent/waybill/");        
        }elseif($roleid>1 && $controllers=='manageadmin'){
            $actionarr = array('index','userdetail','adduser','deluser','taobaoapi'); 
            if(in_array($actions,$actionarr)){
                $this->message("没有权限访问", "", "/admini/manageadmin/backlog/");        
            }
        }
    }

    /**
     * 是否登录
     * @return boolean
     */
    protected function isLogin() {
        if (!array_key_exists(self::COOKIE_USERNAME, $_COOKIE)) {
            //throw new Exception('COOKIE_USERNAME');
            return FALSE;
        }

        if (!array_key_exists(self::COOKIE_IP, $_COOKIE)) {throw new Exception('COOKIE_IP');
            return FALSE;
        }

        if (!array_key_exists(self::COOKIE_TIME, $_COOKIE)) {
            //throw new Exception('COOKIE_TIME');
            return FALSE;
        }

        $username = $this->getUserName();
        if ($username == NULL) {
            //throw new Exception('$username');
            return FALSE;
        }

        

        $lastTime = Uuch_Util::decryptString($_COOKIE[self::COOKIE_TIME]);
        $expriedTime = date('Y-m-d', time() - 24 * 60 * 60 * 30);
        if ($lastTime < $expriedTime) {
            //throw new Exception('$expriedTime');
            return FALSE;
        }


        return TRUE;
    }

    protected function getUserName() {
        return $this->getCurrentUserName();
    }

    public static function getCurrentUserName() {
        if (!array_key_exists(self::COOKIE_USERNAME, $_COOKIE))
            return NULL;
        $__username = $_COOKIE[self::COOKIE_USERNAME];
        return Uuch_Util::decryptString($__username);
    }

    public static function getTopNavigation()
    {
        $type = Application_Model_Navigation::Type_Top;
        $status = Application_Model_Navigation::Status_Launch;
        $parentId = 0;
        return Application_Model_Navigation::getAllByTypeParentId($type,$parentId,$status);
    }
    
    /**
     * 输出提示信息
     * @param type $message 信息
     * @param type $buttonText 按钮名称，不填则显示是返回
     * @param type $url 返回的网址，不填为返回上线
     */
    public function message($message,$buttonText=null,$url=null){
        $this->setMessage($message, $buttonText, $url);
        $this->renderScript('/message.phtml');
    }
    
    /**
     * 设置提示信息
     * @param type $message 信息
     * @param type $buttonText 按钮名称，不填则显示是返回
     * @param type $url 返回的网址，不填为返回上线
     */
    public function setMessage( $message, $buttonText=null, $url=null){
        $this->view->MESSAGE =$message;
        $this->view->BUTTON_TEXT = $buttonText;
        $this->view->URL = $url;
    }

    /**
     * 输出错误信息
     * @param type $message 信息
     * @param type $buttonText 按钮名称，不填则显示是返回
     * @param type $url 返回的网址，不填为返回上线
     */
    public function error( $message, $buttonText=null, $url=NULL){
        $this->setMessage($message, $buttonText, $url);
        $this->renderScript('/error.phtml');
    }
    
    /**
     * 写了日志
     * @param type $money
     * @param type $type
     * @param type $reid
     * @param type $content
     * @param type $userid
     */
    public function writeLog($money,$type,$reid,$content,$userid){
        $operateItem = new Application_Model_Operate();
        $operateItem->operate_name = Uuch_Util::getUserNikeName();
        $operateItem->money = $money;
        $operateItem->type = $type;
        $operateItem->createdate = date("Y-m-d H:i:s");
        $operateItem->reid = $reid;
        $operateItem->content = $content;
        $ip = Application_Model_Operate::get_local_ip();
        $operateItem->ip = $ip;
        $operateItem->userid = $userid;
        $operateItem->save();
    }
    
}